<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css" />
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<style type="text/css">
			html,body{ background: linear-gradient(#23252d 3%, #2a343f 97%);height:100%;  }
			.top { padding-top:110px; text-align:center; }
			.top img { margin:0 auto; width:70px; border-radius: 7px; }
			.top b {display:block; font-size: 18px; font-weight: 500; color:#fff; line-height: 30px; padding-top: 7px;}
			.top p {display:block; font-size: 14px; font-weight: 400; color: #e2e3e6; line-height: 25px; }
			.footer { position: fixed; left: 0; right: 0; bottom: 45px; display:flex; }
			.footer div { width:100%; border:0px red solid; text-align: center;}
			.footer img { width:50px; margin:0 auto; }
			.footer div p { font-size:12px; color:#fff; line-height:35px;}
			.display-none{ display:none; }
			.text-center { text-align: center !important; }
			
		</style>
	</head>
	<body>
		<div class="top">
			<img onerror="javascript:this.src='../../image/defaultimg.png';" src="../../image/defaultimg.png" id="avatar">
			<b id="nickname">--</b>
			<p id="status">正在邀请您语音通话</p>
		</div>
		<div class="footer" id="footer">
			<div tapmode onclick="hangup();">
				<img src="../../image/refuse.png" />
				<p>拒绝</p>
			</div>
			<div tapmode onclick="fnaccept();" class="display-none" id="acceptdiv">
				<img src="../../image/accept.png" />
				<p>接受</p>
			</div>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/app.js"></script>
	<script type="text/javascript" src="../../script/permission.js"></script>
	<script type="text/javascript" src="../../script/doT.js"></script>
	<script type="text/javascript">
		var rong;
		var soundPlayer;
		var dfuid,dfnickname,dfavatar,myuid,myavatar,mynickname,mediaType,callId,opentype,showtime;
		apiready = function() {
			api.parseTapmode();
			soundPlayer = api.require('soundPlayer');
			//api.bringFrameToFront({
				//from: api.frameName
			//});
			rong = api.require('rongCloud2');
			dfuid = api.pageParam.dfuid;
			dfnickname = api.pageParam.dfnickname;
			dfavatar = api.pageParam.dfavatar;
			mediaType = api.pageParam.mediaType;
			callId = api.pageParam.callId;
			opentype = api.pageParam.opentype;
			showtime = api.pageParam.showtime;
			
			var status = '邀请您语音通话';
			if(mediaType == 'video') {
				status = '邀请您视频通话';
			}
			if(opentype == 'receive') {
				$api.removeCls($api.byId('acceptdiv'),'display-none');
				
			}
			else
			{
				status = '正在等待对方接受邀请...';
				api.getPrefs({
					key: 'user_info'
				}, function(ret, err) {
					if (ret && ret.value)
					{
						var userinfo = JSON.parse(ret.value);
						mynickname = userinfo.realname;
						myavatar = userinfo.avatar;
						myuid = userinfo.mobile;
						var extramsg = {"fromuid":myuid,"fromnickname":mynickname,"fromavatar":myavatar,"touid":dfuid,"tonickname":dfnickname,"toavatar":dfavatar,"showtime":showtime};
				
						rong.startCall({
							targetId:dfuid,
							mediaType:mediaType,
							conversationType:'PRIVATE',
							extra:$api.jsonToStr(extramsg),
							userIdList: [dfuid]
						},function(ret){
							console.log('startCall:' + JSON.stringify(ret))
							callId = ret.callSession.callId;
							console.log(callId);
							//fnsetVideoView(0, myuid); 
							
						});
						rong.addCallSessionListener({
							target: 'remoteUserDidJoin'
						}, function(ret) {
							console.log("对端用户加入了通话的事件:" + JSON.stringify(ret));
							if(mediaType == 'video') {
								fnsetVideoView(dfuid);
								setTimeout(function(){
									fnsetVideoView(myuid);
									//1秒后显示本地视频，避免被遮挡
								}, 1000);
							}
							else
							{
								hidebtn();
								showDisbtn();
							}
						});
						rong.addCallSessionListener({
							target: 'didDisconnect'
						}, function(ret) {
							console.log("通话已结束:" + JSON.stringify(ret));
							disConn(ret.disConnectReason);
						});
					}
				});
				
			}
			$api.text($api.byId('status'),status);
			
			if(dfavatar != '') {
				$api.byId('avatar').src = dfavatar;
			}
			$api.text($api.byId('nickname'),dfnickname);
			api.addEventListener({
				name : 'showvideo'
			}, function(ret, err) {
				if(mediaType == 'video') {
					fnsetVideoView(ret.value.uid);
				}
				else
				{
					hidebtn();
					showDisbtn();
				}
			});
			api.addEventListener({
				name : 'rongclouddisconn'
			}, function(ret, err) {
				disConn(ret.value.reason);
				
			});
			api.addEventListener({
				name : 'indexrecive'
			}, function(ret, err) {
				if(dfuid == 'admin' && api.systemType == "android") {
					api.getPrefs({
						key: 'user_info'
					}, function(ret, err) {
						if (ret && ret.value)
						{
							var userinfo = JSON.parse(ret.value);
							myuid = userinfo.mobile;
							
							
							//fnsetVideoView(dfuid);
							setTimeout(function(){
								fnsetVideoView(myuid);
								//1秒后显示本地视频，避免被遮挡
							}, 1000);
						}
					});
				}
				
			});
			
			
			
			playMP3();
		}
		function fnsetVideoView(uid) {
			stopMP3();
			//api.stopPlay();
			//alert(uid+api.systemType+api.winWidth+"!"+api.winHeight);
			var rect = {
				x: 0,
				y: 0,
				w: 'auto',
				h: 'auto'
			};
			if(dfuid != 'admin') {
				if(uid != dfuid) {
					rect = {
						x: 0,
						y: 0,
						w: 120,
						h: 180
					};
				}
			}
			var fname = 'msgrongCloud_frm';
			rong.setVideoView({
				rect: rect,
				userId: uid,
				bg: '#000000',
				//renderModel: 'fit',
				//fixedOn: api.frameName,
				fixed: true
			});
			
			if(uid != dfuid) {
				showDisbtn();
				
				//本地窗口置顶
				rong.videoViewBringToFront({
					userId: myuid
				});
			}
			if(dfuid == 'admin') {
				showDisbtn();
				
			}
		}
		function disConn(reason) {
			//断开连接
			stopMP3();
			//api.stopPlay();
			var reasonstr = '';
			if(reason == 12) {
				reasonstr = '对方已挂断';
			}
			else if(reason == 15) {
				reasonstr = '对方未接听';
			}
			else {
				reasonstr = '连接错误:'+reason;
				reasonstr = '通话结束';
			}
			$api.text($api.byId('status'),reasonstr);
			toast('已断开');
			api.startPlay({
				path: 'widget://res/dong.wav'
			}, function(ret, err) {
				if (ret) {
					setTimeout(function(){
						api.closeFrame({
							name: 'disconnbtn_frm'
						});
						closeframe();
						
					}, 1000);
					
				} else {
					//alert(JSON.stringify(err));
				}
			});
		}
		function hidebtn() {
			//隐藏按钮
			//api.stopPlay();
			stopMP3();
			$api.css($api.byId('footer'),'display:none;');
			$api.text($api.byId('status'),'正在通话...');
		}
		function closeframe() {
			api.closeWin({
			});
		}
		function playMP31() {
			api.startPlay({
				path: 'widget://res/wxring.mp3'
			}, function(ret, err) {
				if (ret) {
					playMP3();
				} else {
					//alert(JSON.stringify(err));
				}
			});
		}
		function playMP3() {
			soundPlayer.play({
				alert: false,
				paths:[{
					  path:'widget://res/wxring.wav',
					  leftVolume:0.8, 
					  rightVolume:0.8, 
					  priority:1, 
					  loop:-1, 
					  rate:1
				}]
			});
			
		}
		function stopMP3() {
			//停止播放来电音效
			soundPlayer.stopSound({
				paths:['widget://res/wxring.wav']
			});
		}
		function fnaccept() {
			//var pername = new Array('phone','microphone','camera');
			var pername = new Array('microphone','camera');
			var perdesc = '麦克风/摄像头';
			if (api.systemType == "android") {
				if(!confirmPerarr(pername,perdesc,'')){
					return;
				}
			}
			//alert(mediaType+'?'+callId);
			//api.stopPlay();
			stopMP3();
			var params = {
				callId: callId
			}
			if (api.systemType == "ios") {
				params = {
					mediaType: mediaType
				}
				
			}
			rong.accept(params);
		}
		function hangup() {
			//挂断
			//api.stopPlay();
			stopMP3();
			rong.hangup();
			
			api.startPlay({
				path: 'widget://res/dong.wav'
			}, function(ret, err) {
				if (ret) {
					setTimeout(function(){
						closeframe();
					}, 1000);
					
				} else {
					//alert(JSON.stringify(err));
				}
			});
		}
		function showDisbtn() {
			//显示挂断按钮
			api.openFrame({
				name: 'disconnbtn_frm',
				url: 'disconnbtn_frm.html',
				bounces: false,
				rect: {
					x: 0,
					y: 0,
					w: 'auto',
					h: 'auto'
				},
				bgColor: 'rgba(0,0,0,0.0)',
				pageParam: {
					mediaType: mediaType
				}
			});	
		}
	</script>
</html>